/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package muddb.entities;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;


/**
 *
 * @author keith
 */
@Entity
@Table( name = "mud_portal" )
@NamedQueries( { @NamedQuery( name = "MudPortal.findByPortId",
            query = "SELECT m FROM MudPortal m WHERE m.portId = :portId" ) } )
public class MudPortal implements Serializable {
  private static final long serialVersionUID = 1L;
  private Integer portId;
  private MudPlace endPlace;
  private MudPlace startPlace;


  public MudPortal() {
  }


  public MudPortal( Integer portId, MudPlace startPlace, MudPlace endPlace ) {
    this.portId = portId;
    this.startPlace = startPlace;
    this.endPlace = endPlace; 
  }


  public MudPortal( Integer portId ) {
    this( portId, null, null );
  }


  public MudPortal( MudPlace startPlace, MudPlace endPlace ) {
    this( null, startPlace, endPlace);
  }


  @Id
  @GeneratedValue( strategy=GenerationType.SEQUENCE )
  @Column( name = "port_id", nullable = false )
  public Integer getPortId() {
    return portId;
  }


  public void setPortId( Integer portId ) {
    this.portId = portId;
  }


  @JoinColumn( name = "end_place_id", nullable = false,
                referencedColumnName = "place_id" )
  @ManyToOne
  public MudPlace getEndPlace() {
    return endPlace;
  }


  public void setEndPlace( MudPlace endPlace ) {
    this.endPlace = endPlace;
  }


  @JoinColumn( name = "start_place_id", nullable = false,
                referencedColumnName = "place_id" )
  @ManyToOne
  public MudPlace getStartPlace() {
    return startPlace;
  }


  public void setStartPlace( MudPlace startPlace ) {
    this.startPlace = startPlace;
  }


  @Override
  public int hashCode() {
    int hash = 0;
    hash += ( portId != null ? portId.hashCode() : 0 );
    return hash;
  }


  @Override
  public boolean equals( Object object ) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if ( !( object instanceof MudPortal ) ) {
      return false;
    }
    MudPortal other = (MudPortal) object;
    if ( ( this.portId == null && other.portId != null ) || ( this.portId != null && !this.portId.equals( other.portId ) ) ) {
      return false;
    }
    return true;
  }


  @Override
  public String toString() {
    return "muddb.entities.MudPortal[portId=" + portId + "]";
  }

  public void connectPlaces(){
    if ( startPlace != null ){
      startPlace.addStart( this );
    }
    if ( endPlace != null ){
      endPlace.addEnd( this );
    }
  }
}
